package com.carbon.trade.controller;

import com.carbon.common.api.Paging;
import com.carbon.domain.common.ApiResult;
import com.carbon.trade.common.BaseController;
import com.carbon.trade.entity.CarbonTradeQuote;
import com.carbon.trade.param.CarbonTradeQuoteQueryParam;
import com.carbon.trade.param.StartTradingParam;
import com.carbon.trade.service.CarbonTradeQuoteService;
import com.carbon.trade.vo.CarbonTradeQuoteQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 碳交易供需行情 前端控制器
 * </p>
 *
 * @author lin rizhao
 * @since 2022-05-21
 */
@Slf4j
@RestController
@RequestMapping("/carbonTradeQuote")
@Api(value = "碳交易供需行情模块", tags = {"碳交易供需行情模块"})
public class CarbonTradeQuoteController extends BaseController {

    @Autowired
    private CarbonTradeQuoteService carbonTradeQuoteService;

    /**
     * 测试接口
     */
    @GetMapping("/test")
    @ApiOperation(value = "测试接口", notes = "测试接口")
    public ApiResult<String> test() {
        return ApiResult.ok("CarbonTradeQuoteController is working!");
    }

    /**
     * 分页查询碳交易供需行情
     */
    @PostMapping("/getPageList")
    @ApiOperation(value = "分页查询碳交易供需行情", notes = "分页查询碳交易供需行情")
    public ApiResult<Paging<CarbonTradeQuoteQueryVo>> getPageList(@RequestBody CarbonTradeQuoteQueryParam queryParam) {
        try {
            log.info("查询碳交易供需行情参数: {}", queryParam);
            Paging<CarbonTradeQuoteQueryVo> paging = carbonTradeQuoteService.getPageList(queryParam);
            return ApiResult.ok(paging);
        } catch (Exception e) {
            log.error("查询碳交易供需行情失败", e);
            return ApiResult.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 添加碳交易供需行情
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加碳交易供需行情", notes = "添加碳交易供需行情")
    public ApiResult<Boolean> add(@Valid @RequestBody CarbonTradeQuote carbonTradeQuote) {
        try {
            log.info("添加碳交易供需行情参数: {}", carbonTradeQuote);
            boolean result = carbonTradeQuoteService.save(carbonTradeQuote);
            return ApiResult.ok(result);
        } catch (Exception e) {
            log.error("添加碳交易供需行情失败", e);
            return ApiResult.fail("添加失败: " + e.getMessage());
        }
    }

    /**
     * 开始交易（报价/询价）
     */
    @PostMapping("/startTrading")
    @ApiOperation(value = "开始交易", notes = "开始交易（报价/询价）")
    public ApiResult<Boolean> startTrading(@Valid @RequestBody StartTradingParam param) {
        try {
            log.info("开始交易参数: {}", param);
            boolean result = carbonTradeQuoteService.startTrading(param);
            return ApiResult.ok(result);
        } catch (Exception e) {
            log.error("开始交易失败", e);
            return ApiResult.fail("开始交易失败: " + e.getMessage());
        }
    }

    /**
     * 搜索关键字
     */
    @PostMapping("/search")
    @ApiOperation(value = "搜索关键字", notes = "搜索关键字")
    public ApiResult<Paging<CarbonTradeQuoteQueryVo>> search(@RequestBody CarbonTradeQuoteQueryParam queryParam) {
        try {
            log.info("搜索碳交易供需行情参数: {}", queryParam);
            Paging<CarbonTradeQuoteQueryVo> paging = carbonTradeQuoteService.getPageList(queryParam);
            return ApiResult.ok(paging);
        } catch (Exception e) {
            log.error("搜索碳交易供需行情失败", e);
            return ApiResult.fail("搜索失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取详情", notes = "根据ID获取碳交易供需行情详情")
    public ApiResult<CarbonTradeQuote> getInfo(@PathVariable("id") Long id) {
        try {
            log.info("获取碳交易供需行情详情ID: {}", id);
            CarbonTradeQuote entity = carbonTradeQuoteService.getById(id);
            return ApiResult.ok(entity);
        } catch (Exception e) {
            log.error("获取碳交易供需行情详情失败", e);
            return ApiResult.fail("获取详情失败: " + e.getMessage());
        }
    }

    // 临时添加询报价管理API，使用现有数据结构
    /**
     * 分页查询碳交易询报价管理（临时实现）
     */
    @PostMapping("/getPricePageList")
    @ApiOperation(value = "分页查询碳交易询报价管理", notes = "分页查询碳交易询报价管理")
    public ApiResult<Paging<CarbonTradeQuoteQueryVo>> getPricePageList(@RequestBody CarbonTradeQuoteQueryParam queryParam) {
        try {
            log.info("查询碳交易询报价管理参数: {}", queryParam);
            Paging<CarbonTradeQuoteQueryVo> paging = carbonTradeQuoteService.getPageList(queryParam);
            return ApiResult.ok(paging);
        } catch (Exception e) {
            log.error("查询碳交易询报价管理失败", e);
            return ApiResult.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 意向成交（临时实现）
     */
    @PostMapping("/intendedTransaction")
    @ApiOperation(value = "意向成交", notes = "意向成交")
    public ApiResult<Boolean> intendedTransaction(@RequestBody Object data) {
        try {
            log.info("意向成交参数: {}", data);
            // 临时返回成功
            return ApiResult.ok(true);
        } catch (Exception e) {
            log.error("意向成交失败", e);
            return ApiResult.fail("意向成交失败: " + e.getMessage());
        }
    }
}

